from fastapi import APIRouter, Depends, Body, UploadFile, Form, Request
from sqlalchemy.ext.asyncio import AsyncSession

from core.dependencies import IdList
from core.response import SuccessResponse, ErrorResponse
from database.database import db_getter
from modules.admin.system import dal, param, schema
from modules.auth import auth


router = APIRouter(
    prefix='/admin/system/dict',
    tags = ['字典管理']
)


###########################################################
#    字典类型管理
###########################################################
@router.get("/types", summary="获取字典类型列表")
async def get_dict_types(params: param.DictTypeParams = Depends(), auth: auth.Auth = Depends(auth.AllUserAuth())):
    datas, count = await dal.DictTypeDal(auth.db).get_datas(**params.dict(), v_return_count=True)
    return SuccessResponse(datas, count=count)


@router.post("/types", summary="创建字典类型")
async def create_dict_types(data: schema.DictType, auth: auth.Auth = Depends(auth.AllUserAuth())):
    return SuccessResponse(await dal.DictTypeDal(auth.db).create_data(data=data))


@router.delete("/types", summary="批量删除字典类型")
async def delete_dict_types(ids: IdList = Depends(), auth: auth.Auth = Depends(auth.AllUserAuth())):
    await dal.DictTypeDal(auth.db).delete_datas(ids=ids.ids)
    return SuccessResponse("删除成功")


@router.post("/types/details", summary="获取多个字典类型下的字典元素列表")
async def post_dicts_details(
        auth: auth.Auth = Depends(auth.AllUserAuth()),
        dict_types: list[str] = Body(None, title="字典元素列表", description="查询字典元素列表")
):
    datas = await dal.DictTypeDal(auth.db).get_dicts_details(dict_types)
    return SuccessResponse(datas)


@router.get("/types/options", summary="获取字典类型选择项")
async def get_dicts_options(auth: auth.Auth = Depends(auth.AllUserAuth())):
    return SuccessResponse(await dal.DictTypeDal(auth.db).get_select_datas())


@router.put("/types/{data_id}", summary="更新字典类型")
async def put_dict_types(data_id: int, data: schema.DictType, auth: auth.Auth = Depends(auth.AllUserAuth())):
    return SuccessResponse(await dal.DictTypeDal(auth.db).put_data(data_id, data))


@router.get("/types/{data_id}", summary="获取字典类型详细")
async def get_dict_type(data_id: int, auth: auth.Auth = Depends(auth.AllUserAuth())):
    return SuccessResponse(await dal.DictTypeDal(auth.db).get_data(data_id, v_schema=schema.DictTypeSimpleOut))


###########################################################
#    字典元素管理
###########################################################
@router.post("/details", summary="创建字典元素")
async def create_dict_details(data: schema.DictDetails, auth: auth.Auth = Depends(auth.AllUserAuth())):
    return SuccessResponse(await dal.DictDetailsDal(auth.db).create_data(data=data))


@router.get("/details", summary="获取单个字典类型下的字典元素列表，分页")
async def get_dict_details(params: param.DictDetailParams = Depends(), auth: auth.Auth = Depends(auth.AllUserAuth())):
    datas, count = await dal.DictDetailsDal(auth.db).get_datas(**params.dict(), v_return_count=True)
    return SuccessResponse(datas, count=count)


@router.delete("/details", summary="批量删除字典元素", description="硬删除")
async def delete_dict_details(ids: IdList = Depends(), auth: auth.Auth = Depends(auth.AllUserAuth())):
    await dal.DictDetailsDal(auth.db).delete_datas(ids.ids, v_soft=False)
    return SuccessResponse("删除成功")


@router.put("/details/{data_id}", summary="更新字典元素")
async def put_dict_details(data_id: int, data: schema.DictDetails, auth: auth.Auth = Depends(auth.AllUserAuth())):
    return SuccessResponse(await dal.DictDetailsDal(auth.db).put_data(data_id, data))


@router.get("/details/{data_id}", summary="获取字典元素详情")
async def get_dict_detail(data_id: int, auth: auth.Auth = Depends(auth.AllUserAuth())):
    return SuccessResponse(await dal.DictDetailsDal(auth.db).get_data(data_id, v_schema=schema.DictDetailsSimpleOut))

